using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Common;
using Common.NotifyEvent;

namespace SProject.Paramter
{
	[Serializable]
	public class BasicOption : NotifyPropertyChanged
	{
		private bool _IsDetectionReadOnly;

		private bool _IsBasicReadOnly;

		public bool IsDetectionReadOnly
		{
			get
			{
				return _IsDetectionReadOnly;
			}
			set
			{
				if (_IsDetectionReadOnly != value)
				{
					_IsDetectionReadOnly = value;
					OnPropertyChanged("IsDetectionReadOnly");
				}
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return _IsBasicReadOnly;
			}
			set
			{
				if (_IsBasicReadOnly != value)
				{
					_IsBasicReadOnly = value;
					OnPropertyChanged("IsReadOnly");
				}
			}
		}

		public ObservableCollection<BasicOptionItem> Items { get; protected set; }

		public BasicOption()
		{
			Items = new ObservableCollection<BasicOptionItem>();
		}

		public BasicOptionItem GetItem(int tubeNo, int channelNo)
		{
			return Items.Where((BasicOptionItem s) => s.TubeNo == tubeNo && s.ChannelNo == channelNo).FirstOrDefault();
		}

		public BasicOptionItem GetItem(string targetName)
		{
			return Items.Where((BasicOptionItem s) => s.TargetName == targetName).FirstOrDefault();
		}

		public virtual void Copy(BasicOption basicOption)
		{
			IsDetectionReadOnly = basicOption.IsDetectionReadOnly;
			IsReadOnly = basicOption.IsReadOnly;
			Items.Clear();
			for (int i = 0; i < basicOption.Items.Count; i++)
			{
				BasicOptionItem basicOptionItem = CreateBasicOptionItem();
				basicOptionItem.Copy(basicOption.Items[i]);
				Items.Add(basicOptionItem);
			}
		}

		protected virtual BasicOptionItem CreateBasicOptionItem()
		{
			return new BasicOptionItem();
		}

		public bool Equals(BasicOption bo)
		{
			if (bo == null)
			{
				return false;
			}
			if (base.Equals(bo))
			{
				return true;
			}
			if (IsDetectionReadOnly != bo.IsDetectionReadOnly || IsReadOnly != bo.IsReadOnly || Items.Count != bo.Items.Count)
			{
				return false;
			}
			for (int i = 0; i < Items.Count; i++)
			{
				if (!Items[i].Equals(bo.Items[i]))
				{
					return false;
				}
			}
			return true;
		}

		public void AddBasicOptionItem(int tubeNo, int channelNo, string targetName, string tubeName)
		{
			BasicOptionItem basicOptionItem = CreateBasicOptionItem();
			basicOptionItem.TubeNo = tubeNo;
			basicOptionItem.ChannelNo = channelNo;
			basicOptionItem.TargetName = targetName;
			basicOptionItem.TubeName = tubeName;
			int index = Items.Count;
			for (int i = 0; i < Items.Count; i++)
			{
				if (basicOptionItem.TubeNo == Items[i].TubeNo)
				{
					Items[i].TubeName = basicOptionItem.TubeName;
					if (basicOptionItem.ChannelNo == Items[i].ChannelNo)
					{
						Items[i].TargetName = basicOptionItem.TargetName;
						return;
					}
					if (basicOptionItem.ChannelNo < Items[i].ChannelNo)
					{
						index = i;
						break;
					}
				}
				else if (basicOptionItem.TubeNo < Items[i].TubeNo)
				{
					index = i;
					break;
				}
			}
			Items.Insert(index, basicOptionItem);
		}

		public void DeleteBasicOptionItem(int channelNo, int tubeNo)
		{
			foreach (BasicOptionItem item in Items)
			{
				if (item.ChannelNo == channelNo && item.TubeNo == tubeNo)
				{
					Items.Remove(item);
					break;
				}
			}
		}

		public virtual void SaveToStream(StreamEx stream)
		{
			stream.Write(PropertyKey.Key_ProjectBasicOption);
			List<PropertyKey> list = new List<PropertyKey>();
			List<byte[]> list2 = new List<byte[]>();
			StreamEx streamEx = new StreamEx();
			streamEx.Write(BytesConverter.GetBytes(Items.Count));
			foreach (BasicOptionItem item in Items)
			{
				item.SaveToStream(streamEx);
			}
			list.Add(PropertyKey.Key_Items);
			list2.Add(streamEx.ToArray());
			streamEx.Close();
			list.Add(PropertyKey.Key_IsReadOnly);
			list2.Add(BytesConverter.GetBytes(IsReadOnly));
			list.Add(PropertyKey.Key_IsDetectionReadOnly);
			list2.Add(BytesConverter.GetBytes(IsDetectionReadOnly));
			stream.Write(PropertyKey.Key_Index);
			stream.WriteIndex(list, list2);
			stream.Flush();
		}

		public virtual bool ReadFromStream(StreamEx stream)
		{
			Items.Clear();
			PropertyKey propertyKey = stream.ReadKey();
			if (propertyKey == PropertyKey.Key_ProjectBasicOption)
			{
				propertyKey = stream.ReadKey();
				if (propertyKey == PropertyKey.Key_Index)
				{
					List<PropertyKey> keys;
					List<byte[]> dates;
					stream.ReadIndex(out keys, out dates);
					for (int i = 0; i < keys.Count; i++)
					{
						switch (keys[i])
						{
						case PropertyKey.Key_Items:
						{
							StreamEx streamEx = new StreamEx();
							streamEx.Write(dates[i]);
							streamEx.Seek(0L, SeekOrigin.Begin);
							int num = streamEx.ReadInt();
							for (int j = 0; j < num; j++)
							{
								BasicOptionItem basicOptionItem = CreateBasicOptionItem();
								basicOptionItem.ReadFromStream(streamEx);
								Items.Add(basicOptionItem);
							}
							streamEx.Close();
							break;
						}
						case PropertyKey.Key_IsReadOnly:
							IsReadOnly = BytesConverter.BytesToBool(dates[i]);
							break;
						case PropertyKey.Key_IsDetectionReadOnly:
							IsDetectionReadOnly = BytesConverter.BytesToBool(dates[i]);
							break;
						}
					}
					return true;
				}
				stream.Seek(-4L, SeekOrigin.Current);
				int num2 = stream.ReadInt();
				for (int k = 0; k < num2; k++)
				{
					BasicOptionItem basicOptionItem2 = CreateBasicOptionItem();
					basicOptionItem2.ReadFromStream(stream);
					Items.Add(basicOptionItem2);
				}
				return true;
			}
			stream.Seek(-4L, SeekOrigin.Current);
			return false;
		}
	}
}
